package org.jeecg.modules.charge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.charge.ChargeBills;
import org.jeecg.modules.charge.entity.*;
import org.jeecg.modules.charge.service.impl.ChargeBillsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
* @Description: 缴费通知
* @Author: jeecg-boot
* @Date:   2019-08-08
* @Version: V1.0
*/
@Slf4j
@Api(tags="催缴管理")
@RestController
@RequestMapping("/b/charge/prompt")
public class ChargePromptController {
   @Autowired
   private ChargeBillsServiceImpl chargeBillsService;
   /**
     * 账单列表查询
    * @param chargeBills
    * @param req
    * @return
    */
   @AutoLog(value = "账单列表查询")
   @ApiOperation(value="账单列表查询", notes="账单列表查询")
   @GetMapping(value = "/list")
   public Result<List<ChargeBills>> queryPageList(ChargeBills chargeBills,HttpServletRequest req) {
       Result<List<ChargeBills>> result = new Result<List<ChargeBills>>();
       try {
           List<ChargeBills> list = new ArrayList<ChargeBills>();
           if (StringUtils.isNotEmpty(chargeBills.getCustomerId())) {
               QueryWrapper<ChargeBills> queryWrapper = QueryGenerator.initQueryWrapper(chargeBills, req.getParameterMap(), false);
               queryWrapper.isNull("zj_skrq");
               queryWrapper.eq("del_flag","0");
               queryWrapper.orderByAsc("zj_cny");
               list = chargeBillsService.list(queryWrapper);
           }
           result.setSuccess(true);
           result.setResult(list);
       }catch (Exception e) {
           log.error(e.getMessage(),e);
           result.error500("操作失败");
       }
       return result;
   }

   /**
     *   添加
    * @param chargeBills
    * @return
    */
   @AutoLog(value = "账单-添加")
   @ApiOperation(value="账单-添加", notes="账单-添加")
   @PostMapping(value = "/add")
   public Result<ChargeBills> add(@RequestBody ChargeBills chargeBills) {
       Result<ChargeBills> result = new Result<ChargeBills>();
       try {
           chargeBillsService.save(chargeBills);
           result.success("添加成功！");
       } catch (Exception e) {
           log.error(e.getMessage(),e);
           result.error500("操作失败");
       }
       return result;
   }

   /**
     *  编辑
    * @param chargeBills
    * @return
    */
   @AutoLog(value = "账单-编辑")
   @ApiOperation(value="账单-编辑", notes="账单-编辑")
   @PutMapping(value = "/edit")
   public Result<ChargeBills> edit(@RequestBody ChargeBills chargeBills) {
       Result<ChargeBills> result = new Result<ChargeBills>();
       ChargeBills chargeBillsEntity = chargeBillsService.getById(chargeBills.getId());
       if(chargeBillsEntity==null) {
           result.error500("未找到对应实体");
       }else {
           chargeBills.setZjTzrq(new Date());
           boolean ok = chargeBillsService.updateById(chargeBills);
           //TODO 返回false说明什么？
           if(ok) {
               result.success("修改成功!");
           }
       }

       return result;
   }
}
